package com.example.adminuser.service;

import com.example.adminuser.dao.AdminUserMapper;
import com.example.adminuser.entity.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 13:50 2019/4/4
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public AdminUser adminUserLogin(AdminUser adminUser) {
          AdminUser currentUser=adminUserMapper.selectAdminByUserName(adminUser.getAdminUserUsername());
          if (passwordEncoder.matches(adminUser.getAdminUserPassword(),currentUser.getAdminUserPassword())){
              return currentUser;
          }else {
              return null;
          }

    }

    @Override
    public List<AdminUser> selectAdminUserListPage(int offset, int limit) {
        return adminUserMapper.selectAdminUserListPage(offset,limit);
    }

    @Override
    public int selectAdminUserPageCount() {
        return adminUserMapper.selectAdminUserPageCount();
    }

    @Override
    public void adminUserAdd(AdminUser adminUser) {
        adminUser.setAdminUserRole("管理员");
        adminUser.setAdminUserPassword(passwordEncoder.encode(adminUser.getAdminUserPassword()));
        adminUserMapper.insert(adminUser);
    }

    @Override
    public void adminUserDelete(AdminUser adminUser) {
        adminUserMapper.deleteByPrimaryKey(adminUser.getAdminUserId());
    }

    @Override
    public void modifyPassword(AdminUser adminUser) {
        adminUser.setAdminUserPassword(passwordEncoder.encode(adminUser.getAdminUserPassword()));
        adminUserMapper.updateByPrimaryKeySelective(adminUser);
    }
}
